<!DOCTYPE html>
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
  <meta name="description" content="刘清政">
  <meta name="keyword" content="hexo-theme">
  
    <link rel="shortcut icon" href="/css/images/logo.png">
  
  <title>
    
      05-常量 | Justin-刘清政的博客
    
  </title>
  <link href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/tomorrow.min.css" rel="stylesheet">
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/css/plugins/gitment.css">

  
  <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/geopattern/1.2.3/js/geopattern.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.js"></script>
  
    
<script src="/js/qrious.js"></script>

  
  
    
<script src="/js/gitment.js"></script>

  
  

  
<meta name="generator" content="Hexo 4.2.0"></head>
<div class="wechat-share">
  <img src="/css/images/logo.png" />
</div>

  <body>
    <header class="header fixed-header">
  <div class="header-container">
    <a class="home-link" href="/">
      <div class="logo"></div>
      <span>Justin-刘清政的博客</span>
    </a>
    <ul class="right-list">
      
        <li class="list-item">
          
            <a href="/" class="item-link">主页</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/tags/" class="item-link">标签</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/archives/" class="item-link">归档</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/about/" class="item-link">关于我</a>
          
        </li>
      
    </ul>
    <div class="menu">
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
    </div>
    <div class="menu-mask">
      <ul class="menu-list">
        
          <li class="menu-item">
            
              <a href="/" class="menu-link">主页</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/tags/" class="menu-link">标签</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/archives/" class="menu-link">归档</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/about/" class="menu-link">关于我</a>
            
          </li>
        
      </ul>
    </div>
  </div>
</header>

    <div id="article-banner">
  <h2>05-常量</h2>



  <p class="post-date">2019-03-10</p>
    <!-- 不蒜子统计 -->
    <span id="busuanzi_container_page_pv" style='display:none' class="">
        <i class="icon-smile icon"></i> 阅读数：<span id="busuanzi_value_page_pv"></span>次
    </span>
  <div class="arrow-down">
    <a href="javascript:;"></a>
  </div>
</div>
<main class="app-body flex-box">
  <!-- Article START -->
  <article class="post-article">
    <section class="markdown-content"><h3 id="定义"><a href="#定义" class="headerlink" title="定义"></a>定义</h3><p>在 Go 语言中，术语”常量”用于表示固定的值。比如 <code>5</code> 、<code>-89</code>、 <code>I love Go</code>、<code>67.89</code> 等等。</p>
<p>看看下面的代码:</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a <span class="keyword">int</span> = <span class="number">50</span>  </span><br><span class="line"><span class="keyword">var</span> b <span class="keyword">string</span> = <span class="string">"I love Go"</span></span><br></pre></td></tr></table></figure>

<p><strong>在上面的代码中，变量 a 和 b 分别被赋值为常量 50 和 I love GO</strong>。关键字 <code>const</code> 被用于表示常量，比如 <code>50</code> 和 <code>I love Go</code>。即使在上面的代码中我们没有明确的使用关键字 <code>const</code>，但是在 Go 的内部，它们是常量。</p>
<p>顾名思义，常量不能再重新赋值为其他的值。因此下面的程序将不能正常工作，它将出现一个编译错误: <code>cannot assign to a.</code>。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;  </span><br><span class="line">    <span class="keyword">const</span> a = <span class="number">55</span> <span class="comment">// 允许</span></span><br><span class="line">    a = <span class="number">89</span>       <span class="comment">// 不允许重新赋值</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>常量的值会在编译的时候确定。因为函数调用发生在运行时，所以不能将函数的返回值赋值给常量。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (  </span><br><span class="line">    <span class="string">"fmt"</span></span><br><span class="line">    <span class="string">"math"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;  </span><br><span class="line">    fmt.Println(<span class="string">"Hello, playground"</span>)</span><br><span class="line">    <span class="keyword">var</span> a = math.Sqrt(<span class="number">4</span>)   <span class="comment">// 允许</span></span><br><span class="line">    <span class="keyword">const</span> b = math.Sqrt(<span class="number">4</span>) <span class="comment">// 不允许</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在上面的程序中，因为 <code>a</code> 是变量，因此我们可以将函数 <code>math.Sqrt(4)</code> 的返回值赋值给它（我们将在单独的地方详细讨论函数）。</p>
<p> <code>b</code> 是一个常量，它的值需要在编译的时候就确定。函数 <code>math.Sqrt(4)</code> 只会在运行的时候计算，因此 <code>const b = math.Sqrt(4)</code> 将会抛出错误 <code>error main.go:11: const initializer math.Sqrt(4) is not a constant)</code></p>
<h3 id="字符串常量"><a href="#字符串常量" class="headerlink" title="字符串常量"></a>字符串常量</h3><p>双引号中的任何值都是 Go 中的字符串常量。例如像 <code>Hello World</code> 或 <code>Sam</code> 等字符串在 Go 中都是常量。</p>
<p>什么类型的字符串属于常量？答案是他们是无类型的。</p>
<p>像 <code>Hello World</code> 这样的字符串常量没有任何类型。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> hello = <span class="string">"Hello World"</span></span><br></pre></td></tr></table></figure>

<p>上面的例子，我们把 <code>Hello World</code> 分配给常量 <code>hello</code>。现在常量 <code>hello</code> 有类型吗？答案是没有。常量仍然没有类型。</p>
<p>Go 是一门强类型语言，所有的变量必须有明确的类型。那么, 下面的程序是如何将无类型的常量 <code>Sam</code> 赋值给变量 <code>name</code> 的呢？</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (  </span><br><span class="line">    <span class="string">"fmt"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;  </span><br><span class="line">    <span class="keyword">var</span> name = <span class="string">"Sam"</span></span><br><span class="line">    fmt.Printf(<span class="string">"type %T value %v"</span>, name, name)</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>答案是无类型的常量有一个与它们相关联的默认类型，并且当且仅当一行代码需要时才提供它。在声明中 var name = “Sam” ， name 需要一个类型，它从字符串常量 Sam 的默认类型中获取。</strong></p>
<p>有没有办法创建一个带类型的常量？答案是可以的。以下代码创建一个有类型常量。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> typedhello <span class="keyword">string</span> = <span class="string">"Hello World"</span></span><br></pre></td></tr></table></figure>

<p>上面代码中， <code>typedhello</code> 就是一个 <code>string</code> 类型的常量。</p>
<p>Go 是一个强类型的语言，在分配过程中混合类型是不允许的。让我们通过以下程序看看这句话是什么意思。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;  </span><br><span class="line">        <span class="keyword">var</span> defaultName = <span class="string">"Sam"</span> <span class="comment">// 允许</span></span><br><span class="line">        <span class="keyword">type</span> myString <span class="keyword">string</span></span><br><span class="line">        <span class="keyword">var</span> customName myString = <span class="string">"Sam"</span> <span class="comment">// 允许</span></span><br><span class="line">        customName = defaultName <span class="comment">// 不允许</span></span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在上面的代码中，我们首先创建一个变量 <code>defaultName</code> 并分配一个常量 <code>Sam</code> 。<strong>常量 Sam 的默认类型是 string ，所以在赋值后 defaultName 是 string 类型的。</strong></p>
<p>下一行，我们将创建一个新类型 <code>myString</code>，它是 <code>string</code> 的别名。</p>
<p>然后我们创建一个 <code>myString</code> 的变量 <code>customName</code> 并且给他赋值一个常量 <code>Sam</code> 。因为常量 <code>Sam</code> 是无类型的，它可以分配给任何字符串变量。因此这个赋值是允许的，<code>customName</code> 的类型是 <code>myString</code>。</p>
<p>现在，我们有一个类型为 <code>string</code> 的变量 <code>defaultName</code> 和另一个类型为 <code>myString</code> 的变量 <code>customName</code>。即使我们知道这个 <code>myString</code> 是 <code>string</code> 类型的别名。Go 的类型策略不允许将一种类型的变量赋值给另一种类型的变量。因此将 <code>defaultName</code> 赋值给 <code>customName</code> 是不允许的，编译器会抛出一个错误 <code>main.go:7:20: cannot use defaultName (type string) as type myString in assignmen</code>。</p>
<h3 id="布尔常量"><a href="#布尔常量" class="headerlink" title="布尔常量"></a>布尔常量</h3><p>布尔常量和字符串常量没有什么不同。他们是两个无类型的常量 <code>true</code> 和 <code>false</code>。字符串常量的规则适用于布尔常量，所以在这里我们不再重复。以下是解释布尔常量的简单程序。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;  </span><br><span class="line">    <span class="keyword">const</span> trueConst = <span class="literal">true</span></span><br><span class="line">    <span class="keyword">type</span> myBool <span class="keyword">bool</span></span><br><span class="line">    <span class="keyword">var</span> defaultBool = trueConst <span class="comment">// 允许</span></span><br><span class="line">    <span class="keyword">var</span> customBool myBool = trueConst <span class="comment">// 允许</span></span><br><span class="line">    defaultBool = customBool <span class="comment">// 不允许</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>上面的程序是自我解释的。</p>
<h3 id="数字常量"><a href="#数字常量" class="headerlink" title="数字常量"></a>数字常量</h3><p>数字常量包含整数、浮点数和复数的常量。数字常量中有一些微妙之处。</p>
<p>让我们看一些例子来说清楚。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (  </span><br><span class="line">    <span class="string">"fmt"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;  </span><br><span class="line">    <span class="keyword">const</span> a = <span class="number">5</span></span><br><span class="line">    <span class="keyword">var</span> intVar <span class="keyword">int</span> = a</span><br><span class="line">    <span class="keyword">var</span> int32Var <span class="keyword">int32</span> = a</span><br><span class="line">    <span class="keyword">var</span> float64Var <span class="keyword">float64</span> = a</span><br><span class="line">    <span class="keyword">var</span> complex64Var <span class="keyword">complex64</span> = a</span><br><span class="line">    fmt.Println(<span class="string">"intVar"</span>,intVar, <span class="string">"\nint32Var"</span>, int32Var, <span class="string">"\nfloat64Var"</span>, float64Var, <span class="string">"\ncomplex64Var"</span>,complex64Var)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>上面的程序，常量 <code>a</code> 是没有类型的，它的值是 <code>5</code> 。您可能想知道 <code>a</code> 的默认类型是什么，如果它确实有一个的话, 那么我们如何将它分配给不同类型的变量。答案在于 <code>a</code> 的语法。下面的程序将使事情更加清晰。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (  </span><br><span class="line">    <span class="string">"fmt"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;  </span><br><span class="line">    <span class="keyword">var</span> i = <span class="number">5</span></span><br><span class="line">    <span class="keyword">var</span> f = <span class="number">5.6</span></span><br><span class="line">    <span class="keyword">var</span> c = <span class="number">5</span> + <span class="number">6i</span></span><br><span class="line">    fmt.Printf(<span class="string">"i's type %T, f's type %T, c's type %T"</span>, i, f, c)</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在上面的程序中，每个变量的类型由数字常量的语法决定。<code>5</code> 在语法中是整数， <code>5.6</code> 是浮点数，<code>5+6i</code> 的语法是复数。当我们运行上面的程序，它会打印出 <code>i&#39;s type int, f&#39;s type float64, c&#39;s type complex128</code>。</p>
<p>现在我希望下面的程序能够正确的工作。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (  </span><br><span class="line">    <span class="string">"fmt"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;  </span><br><span class="line">    <span class="keyword">const</span> a = <span class="number">5</span></span><br><span class="line">    <span class="keyword">var</span> intVar <span class="keyword">int</span> = a</span><br><span class="line">    <span class="keyword">var</span> int32Var <span class="keyword">int32</span> = a</span><br><span class="line">    <span class="keyword">var</span> float64Var <span class="keyword">float64</span> = a</span><br><span class="line">    <span class="keyword">var</span> complex64Var <span class="keyword">complex64</span> = a</span><br><span class="line">    fmt.Println(<span class="string">"intVar"</span>,intVar, <span class="string">"\nint32Var"</span>, int32Var, <span class="string">"\nfloat64Var"</span>, float64Var, <span class="string">"\ncomplex64Var"</span>,complex64Var)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在这个程序中， <code>a</code> 的值是 <code>5</code> ，<code>a</code> 的语法是通用的（它可以代表一个浮点数、整数甚至是一个没有虚部的复数），因此可以将其分配给任何兼容的类型。这些常量的默认类型可以被认为是根据上下文在运行中生成的。 <code>var intVar int = a</code> 要求 <code>a</code> 是 <code>int</code>，所以它变成一个 <code>int</code> 常量。 <code>var complex64Var complex64 = a</code> 要求 <code>a</code> 是 <code>complex64</code>，因此它变成一个复数类型。很简单的:)。</p>
<h3 id="数字表达式"><a href="#数字表达式" class="headerlink" title="数字表达式"></a>数字表达式</h3><p>数字常量可以在表达式中自由混合和匹配，只有当它们被分配给变量或者在需要类型的代码中的任何地方使用时，才需要类型。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (  </span><br><span class="line">    <span class="string">"fmt"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;  </span><br><span class="line">    <span class="keyword">var</span> a = <span class="number">5.9</span>/<span class="number">8</span></span><br><span class="line">    fmt.Printf(<span class="string">"a's type %T value %v"</span>,a, a)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在上面的程序中， <code>5.9</code> 在语法中是浮点型，<code>8</code> 是整型，<code>5.9/8</code> 是允许的，因为两个都是数字常量。除法的结果是 <code>0.7375</code> 是一个浮点型，所以 <code>a</code> 的类型是浮点型。这个程序的输出结果是: <code>a&#39;s type float64 value 0.7375</code>。</p>
</section>
    <!-- Tags START -->
    
      <div class="tags">
        <span>Tags:</span>
        
  <a href="/tags#Go" >
    <span class="tag-code">Go</span>
  </a>

      </div>
    
    <!-- Tags END -->
    <!-- NAV START -->
    
  <div class="nav-container">
    <!-- reverse left and right to put prev and next in a more logic postition -->
    
      <a class="nav-left" href="/go/2-Go%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/03-%E5%8F%98%E9%87%8F/">
        <span class="nav-arrow">← </span>
        
          3-变量
        
      </a>
    
    
      <a class="nav-right" href="/go/2-Go%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/06-%E5%87%BD%E6%95%B0%EF%BC%88Function%EF%BC%89/">
        
          6-函数（Function
        
        <span class="nav-arrow"> →</span>
      </a>
    
  </div>

    <!-- NAV END -->
    <!-- 打赏 START -->
    
      <div class="money-like">
        <div class="reward-btn">
          赏
          <span class="money-code">
            <span class="alipay-code">
              <div class="code-image"></div>
              <b>使用支付宝打赏</b>
            </span>
            <span class="wechat-code">
              <div class="code-image"></div>
              <b>使用微信打赏</b>
            </span>
          </span>
        </div>
        <p class="notice">点击上方按钮,请我喝杯咖啡！</p>
      </div>
    
    <!-- 打赏 END -->
    <!-- 二维码 START -->
    
      <div class="qrcode">
        <canvas id="share-qrcode"></canvas>
        <p class="notice">扫描二维码，分享此文章</p>
      </div>
    
    <!-- 二维码 END -->
    
      <!-- Gitment START -->
      <div id="comments"></div>
      <!-- Gitment END -->
    
  </article>
  <!-- Article END -->
  <!-- Catalog START -->
  
    <aside class="catalog-container">
  <div class="toc-main">
  <!-- 不蒜子统计 -->
    <strong class="toc-title">目录</strong>
    
      <ol class="toc-nav"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#定义"><span class="toc-nav-text">定义</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#字符串常量"><span class="toc-nav-text">字符串常量</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#布尔常量"><span class="toc-nav-text">布尔常量</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#数字常量"><span class="toc-nav-text">数字常量</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#数字表达式"><span class="toc-nav-text">数字表达式</span></a></li></ol>
    
  </div>
</aside>
  
  <!-- Catalog END -->
</main>

<script>
  (function () {
    var url = 'http://www.liuqingzheng.top/go/2-Go语言基础/05-常量/';
    var banner = ''
    if (banner !== '' && banner !== 'undefined' && banner !== 'null') {
      $('#article-banner').css({
        'background-image': 'url(' + banner + ')'
      })
    } else {
      $('#article-banner').geopattern(url)
    }
    $('.header').removeClass('fixed-header')

    // error image
    $(".markdown-content img").on('error', function() {
      $(this).attr('src', 'http://file.muyutech.com/error-img.png')
      $(this).css({
        'cursor': 'default'
      })
    })

    // zoom image
    $(".markdown-content img").on('click', function() {
      var src = $(this).attr('src')
      if (src !== 'http://file.muyutech.com/error-img.png') {
        var imageW = $(this).width()
        var imageH = $(this).height()

        var zoom = ($(window).width() * 0.95 / imageW).toFixed(2)
        zoom = zoom < 1 ? 1 : zoom
        zoom = zoom > 2 ? 2 : zoom
        var transY = (($(window).height() - imageH) / 2).toFixed(2)

        $('body').append('<div class="image-view-wrap"><div class="image-view-inner"><img src="'+ src +'" /></div></div>')
        $('.image-view-wrap').addClass('wrap-active')
        $('.image-view-wrap img').css({
          'width': `${imageW}`,
          'transform': `translate3d(0, ${transY}px, 0) scale3d(${zoom}, ${zoom}, 1)`
        })
        $('html').css('overflow', 'hidden')

        $('.image-view-wrap').on('click', function() {
          $(this).remove()
          $('html').attr('style', '')
        })
      }
    })
  })();
</script>


  <script>
    var qr = new QRious({
      element: document.getElementById('share-qrcode'),
      value: document.location.href
    });
  </script>



  <script>
    var gitmentConfig = "liuqingzheng";
    if (gitmentConfig !== 'undefined') {
      var gitment = new Gitment({
        id: "05-常量",
        owner: "liuqingzheng",
        repo: "FuckBlog",
        oauth: {
          client_id: "32a4076431cf39d0ecea",
          client_secret: "94484bd79b3346a949acb2fda3c8a76ce16990c6"
        },
        theme: {
          render(state, instance) {
            const container = document.createElement('div')
            container.lang = "en-US"
            container.className = 'gitment-container gitment-root-container'
            container.appendChild(instance.renderHeader(state, instance))
            container.appendChild(instance.renderEditor(state, instance))
            container.appendChild(instance.renderComments(state, instance))
            container.appendChild(instance.renderFooter(state, instance))
            return container;
          }
        }
      })
      gitment.render(document.getElementById('comments'))
    }
  </script>




    <div class="scroll-top">
  <span class="arrow-icon"></span>
</div>
    <footer class="app-footer">
<!-- 不蒜子统计 -->
<span id="busuanzi_container_site_pv">
     本站总访问量<span id="busuanzi_value_site_pv"></span>次
</span>
<span class="post-meta-divider">|</span>
<span id="busuanzi_container_site_uv" style='display:none'>
     本站访客数<span id="busuanzi_value_site_uv"></span>人
</span>
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>



  <p class="copyright">
    &copy; 2021 | Proudly powered by <a href="https://www.cnblogs.com/xiaoyuanqujing" target="_blank">小猿取经</a>
    <br>
    Theme by <a href="https://www.cnblogs.com/xiaoyuanqujing" target="_blank" rel="noopener">小猿取经</a>
  </p>
</footer>

<script>
  function async(u, c) {
    var d = document, t = 'script',
      o = d.createElement(t),
      s = d.getElementsByTagName(t)[0];
    o.src = u;
    if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
    s.parentNode.insertBefore(o, s);
  }
</script>
<script>
  async("//cdnjs.cloudflare.com/ajax/libs/fastclick/1.0.6/fastclick.min.js", function(){
    FastClick.attach(document.body);
  })
</script>

<script>
  var hasLine = 'true';
  async("//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js", function(){
    $('figure pre').each(function(i, block) {
      var figure = $(this).parents('figure');
      if (hasLine === 'false') {
        figure.find('.gutter').hide();
      }
      var lang = figure.attr('class').split(' ')[1] || 'code';
      var codeHtml = $(this).html();
      var codeTag = document.createElement('code');
      codeTag.className = lang;
      codeTag.innerHTML = codeHtml;
      $(this).attr('class', '').empty().html(codeTag);
      figure.attr('data-lang', lang.toUpperCase());
      hljs.highlightBlock(block);
    });
  })
</script>





<!-- Baidu Tongji -->

<script>
    var _baId = 'c5fd96eee1193585be191f318c3fa725';
    // Originial
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?" + _baId;
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
</script>


<script src="/js/script.js"></script>


<script src="/js/search.js"></script>


<script src="/js/load.js"></script>



  <span class="local-search local-search-google local-search-plugin" style="right: 50px;top: 70px;;position:absolute;z-index:2;">
      <input type="search" placeholder="站内搜索" id="local-search-input" class="local-search-input-cls" style="">
      <div id="local-search-result" class="local-search-result-cls"></div>
  </span>


  </body>
</html>